<template>
 <div>
    hello world
 </div>
</template>

<script>
import { getTreeList } from './api'
export default {
  data() {
   return {
     tree: {}
   }
  },
  methods: {
    async getData() {
      const { data } = await getTreeList()
      console.log(data)
      let allData = [...data.data.parent, ...data.data.child];
      // console.log(allData)
      // 1. 扁平的数据如何变成 多层数据 递归数据
      // { 1: {id: 1, pid: 0, name: '文件夹1'} }
      let treeMapList = allData.reduce((memo, current) => {
        // console.log(memo, current)
        memo[current['id']] = current
        return memo;
      }, {})
      console.log(treeMapList, 'treeMapList')

      let result = allData.reduce((arr, current) => {
        // console.log(arr, current)
        let pid = current.pid
        let parent = treeMapList[pid]
        if(parent) {
          parent.children ? parent.children.push(current) : parent.children = [current]
        } else if(pid === 0) { // 这是跟文件夹
          arr.push(current)
        }
        return arr;
      }, [])

      console.log(result)
    }
  },
  created() {
    this.getData()
  }
}
</script>

<style scoped>
</style>